package com.ling.service;

import com.ling.bean.User;
import com.ling.bean.UserExample;
import com.ling.dao.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Arrays;
import java.util.List;

@Service
public class UserService {
    private final UserMapper userMapper;

    @Autowired
    public UserService(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    //手机号是否存在
    public boolean existTel(String tel) {
        UserExample userExample = new UserExample();
        userExample.createCriteria().andTelEqualTo(tel);
        long count = userMapper.countByExample(userExample);
        return count > 0;
    }

    //用户注册
    public void saveUser(User user) {
        String password_str = user.getPassword();
        password_str = password_str + "zhiyun";
        String password_hash = DigestUtils.md5DigestAsHex(password_str.getBytes()).toUpperCase();
        user.setPassword(password_hash);
        userMapper.insertSelective(user);
    }

    //用户登录
    public boolean checkUser(User user) {
        UserExample userExample = new UserExample();
        String password_str = user.getPassword();
        password_str = password_str + "zhiyun";
        String password_hash = DigestUtils.md5DigestAsHex(password_str.getBytes()).toUpperCase();
        user.setPassword(password_hash);
        userExample.createCriteria().andTelEqualTo(user.getTel()).andPasswordEqualTo(user.getPassword());
        return userMapper.countByExample(userExample)>0;
    }

    //获取用户
    public User getUser(String tel, String password) {
        UserExample userExample = new UserExample();
        userExample.createCriteria().andTelEqualTo(tel).andPasswordEqualTo(password);
        List<User> users = userMapper.selectByExample(userExample);
        if (users.isEmpty()) {
            return null;
        }
        return users.get(0);
    }

    //根据用户手机号和新密码更新密码
    public void changePasswordByTel(String tel, String newPassword) {
        UserExample userExample = new UserExample();
        userExample.createCriteria().andTelEqualTo(tel);
        User user = new User();
        String rawPassword = newPassword + "zhiyun";
        String password_hash = DigestUtils.md5DigestAsHex(rawPassword.getBytes()).toUpperCase();
        user.setPassword(password_hash);
        userMapper.updateByExampleSelective(user, userExample);
    }

    //查询用户,根据用户的jiyan来逆序排列
    public List<User> getUsers() {
        UserExample userExample = new UserExample();
        userExample.setOrderByClause("t_user.jiyan DESC");
        //开始查询
        return userMapper.selectByExample(userExample);
    }

    //保存用户头像
    public void saveHeadImg(String headImgUrl,String userId) {
        User user = new User();
        user.setId(userId);
        user.setHeadimgurl(headImgUrl);
        userMapper.updateByPrimaryKeySelective(user);
    }

    //修改用户信息
    public void updateUserProfile(User user) {
        userMapper.updateByPrimaryKeySelective(user);
    }

    public User getUserById(String id) {
        return userMapper.selectByPrimaryKey(id);
    }

    //获取指定用户关注的所有用户
    public List<User> getFollowingUsers(String userId) {
        return userMapper.getFollowingUsers(userId);
    }

    //获取指定用户的所有粉丝
    public List<User> getFans(String userId) {
        return userMapper.getFans(userId);
    }

    //根据领域id获取排位较前的用户
    public List<String> getUserOrderByAreaId(String areaId) {
        return userMapper.getUserOrderByAreaId(areaId);
    }
}
